package com.twobu.twobuinsight.controller;

import com.twobu.twobuinsight.common.Result;
import com.twobu.twobuinsight.dto.TaskHistoryDTO;
import com.twobu.twobuinsight.dto.TaskHistorySimpleDTO;
import com.twobu.twobuinsight.security.UserContextHolder;
import com.twobu.twobuinsight.service.TaskHistoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/task/history")
@Tag(name = "任务历史", description = "任务历史相关接口")
@Slf4j
@Deprecated
public class TaskHistoryController {

    @Autowired
    private TaskHistoryService taskHistoryService;

    @GetMapping("/list")
    @Operation(summary = "获取当前用户任务历史列表")
    public Result<List<TaskHistorySimpleDTO>> getCurrentUserTaskHistory() {
        // 从用户上下文中获取用户ID
        String userId = UserContextHolder.getUserContext().getUserId().toString();
        log.info("获取当前用户任务历史列表, userId: {}", userId);

        try {
            List<TaskHistorySimpleDTO> historyList = taskHistoryService.getUserTaskHistorySimple(userId);
            return Result.success(historyList);
        } catch (Exception e) {
            log.error("获取用户任务历史列表失败", e);
            return Result.error("获取用户任务历史列表失败: " + e.getMessage());
        }
    }

    @GetMapping("/{taskId}")
    @Operation(summary = "获取任务历史详情")
    public Result<TaskHistoryDTO> getTaskHistoryDetail(@PathVariable String taskId) {
        log.info("获取任务历史详情, taskId: {}", taskId);
        try {
            TaskHistoryDTO taskHistory = taskHistoryService.getTaskHistoryDetail(taskId);
            if (taskHistory == null) {
                return Result.error("任务历史不存在");
            }
            return Result.success(taskHistory);
        } catch (Exception e) {
            log.error("获取任务历史详情失败", e);
            return Result.error("获取任务历史详情失败: " + e.getMessage());
        }
    }
}
